# -*- coding: utf-8 -*-
# Time      : 2021/5/21 - 18:41
# Author    : dingyan
# FileName  : yufan
# Version   : python v3.6.1
import os
import json
import time
import requests
import base64
import yufan

terminal_ip = 'http://192.168.1.56:'
terminal_port = '8090'
terminal_pass = '123456'
person_reg_route = '/person/create'
person_picture_upload_route = '/face/create'
person_delete = '/person/delete'
person_comm_name = '许志强'

batch_picture_local = '/Users/dingyan/Desktop/ssdk/shBatch'
test_picture_local = '/Users/dingyan/Desktop/ssdk/shtest'
test_ok_picture_local = '/Users/dingyan/Desktop/ssdk/shtestOK'

person_ids = []
person_names = []
person_face_pictures = []
success_times = 0
fail_times = 0


def success_or_fail(code, isSuccess):
    if code is 1 and isSuccess:
        yufan.success_times += 1
    else:
        yufan.fail_times += 1



def get_filenames_by_dir(local_path):
    ids = []
    names = []
    files = []
    for root, dirs, files in os.walk(local_path):
        for file in files:
            # print(person_comm_name + os.path.splitext(file)[0])
            if os.path.splitext(file)[1] == '.jpg':
                ids.append(os.path.splitext(file)[0])
                names.append(person_comm_name + os.path.splitext(file)[0])
    return ids, names, files


def create_single_person_json(id=0, name=''):
    str = {
        'id': id,
        'name': name
    }
    ss = json.dumps(str)
    return ss


def send_reg_person(index=0):
    url = terminal_ip + terminal_port + person_reg_route
    # person_ids, person_names, person_face_pictures = get_filenames_by_dir(batch_picture_local)
    body_json = create_single_person_json(person_ids[index], person_names[index])
    heads = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    payload = {
        'pass': terminal_pass,
        'person': body_json
    }
    r = requests.post(url, data=payload, headers=heads)
    print(r.text)


def send_reg_person_picture(index=0):
    url = terminal_ip + terminal_port + person_picture_upload_route
    # person_ids, person_names, person_face_pictures = get_filenames_by_dir(batch_picture_local)
    heads = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    # print(os.path.join(batch_picture_local, person_face_pictures[index]))
    with open(os.path.join(batch_picture_local, person_face_pictures[index]), 'rb') as f:
        image = f.read()
        image_base64 = str(base64.b64encode(image), encoding='utf-8')
        f.close()

        payload = {
            'pass': terminal_pass,
            'personId': person_ids[index],
            'faceId': person_ids[index],
            'imgBase64': image_base64,
            'isEasyWay': False
        }
        r = requests.post(url, data=payload, headers=heads)
        print(r.text)
        dic = json.loads(r.text)
        success_or_fail(dic['result'], dic['success'])


def person_del(ids):
    url = url = terminal_ip + terminal_port + person_delete
    heads = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    payload = {
        'pass': terminal_pass,
        'id': ids
    }
    r = requests.post(url, data=payload, headers=heads)
    print(r.text)

def run():
    yufan.person_ids, yufan.person_names, yufan.person_face_pictures = get_filenames_by_dir(batch_picture_local)

    person_size = len(person_ids)
    counter = 1
    for index in range(person_size):
        print("第 "+ str(counter) +" 次开始……")
        send_reg_person(index)
        time.sleep(0.3)
        print("index==============" + str(index))
        send_reg_person_picture(index)
        time.sleep(0.1)
        print("第 " + str(counter) + " 次结束……")
        counter = counter + 1
    print("Success times = " + yufan.success_times)
    print("Fail times = " + yufan.fail_times)

